package com.youuvs.controller;

import com.youuvs.Dto;
import com.youuvs.JsonResult;
import com.youuvs.domain.Product;
import com.youuvs.domain.Store;
import com.youuvs.feignClient.StoreFeignClient;
import com.youuvs.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private StoreFeignClient storeFeignClient;


    @GetMapping("/getById/{id}")
    public Product getById(@PathVariable("id") Long id){
        return productService.getById(id);
    }

    @PostMapping("/insert")
    public JsonResult insert(@RequestBody Dto dto){
        try {
            //System.out.println("=================================="+name+price+number);
            Product product = new Product();
            product.setName(dto.getName());
            product.setPrice(dto.getPrice());
            System.out.println(product);
            productService.insert(product);
            Store store = new Store();
            store.setProduct_id(product.getId());
            store.setNumber(dto.getNumber());
            System.out.println(store);
            insert(store);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().setMsg("增加失败");
        }



    }
    @PostMapping("/sto/insert")
    public void insert(Store store){
        storeFeignClient.insert(store);
    }

}
